$OpenBSD: patch-src_metamail_metamail_c,v 1.4 2017/05/25 17:47:08 espie Exp $
Index: src/metamail/metamail.c
--- src/metamail/metamail.c.orig
+++ src/metamail/metamail.c
@@ -20,6 +20,9 @@ WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
 
  ******************************************************* */
 #include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -29,7 +32,7 @@ WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
 #ifdef BORLAND
 #define F_OK 0
 extern unsigned _stklen = 16384;
-extern char *mktemp(char *);
+extern char *mkstemp(char *);
 #define WRITE_BINARY	"w"
 #else /* BORLAND */
 #ifdef MICROSOFT
@@ -46,12 +49,14 @@ extern char *mktemp(char *);
 #include <signal.h>
 
 #ifndef AMIGA
-#ifdef SYSV
+#if defined(HAVE_TCGETATTR)
+#include <termios.h>
+#elif defined(SYSV)
 #include <termio.h>
 #include <unistd.h>
-#else /* SYSV */
+#else
 #include <sgtty.h>
-#endif /* SYSV */
+#endif
 #endif /* AMIGA */
 #endif /* MICROSOFT */
 #endif /* BORLAND */
@@ -100,15 +105,12 @@ extern char **environ, *gets();
 #define CMDSIZE 1200 /* Maximum size of command to execute */
 
 #define LINE_BUF_SIZE       2000
-#ifndef MICROSOFT
-extern char *malloc();
-extern char *realloc();
-#endif
-extern char *getenv();
-extern char *index();
-extern char *rindex();
 char fileToDelete[MAX_FILE_NAME_SIZE];
 
+void maybephead(char *);
+void PauseForUser(void);
+
+char *hdr;
 char *FindParam();
 extern FILE *popen();
 static char *nomem = "Out of memory!";
@@ -302,24 +304,6 @@ char **argv;
     int retcode;
 
     modpath(AUXPATH);
-#ifndef MSDOS
-    signal(SIGINT, cleanup);
-#ifndef AMIGA
-    signal(SIGILL, cleanup);
-    signal(SIGTRAP, cleanup);
-    signal(SIGIOT, cleanup);
-    signal(SIGFPE, cleanup);
-#ifndef LINUX
-    signal(SIGEMT, cleanup);
-    signal(SIGBUS, cleanup);
-#endif
-    signal(SIGSEGV, cleanup);
-    signal(SIGTERM, cleanup);
-#endif
-#endif
-#ifdef SIGXCPU
-    signal(SIGXCPU, cleanup);
-#endif
     tmproot = getenv("METAMAIL_TMPDIR");
     if (!tmproot) tmproot="/tmp";
     mailheaders = getenv("MM_HEADERS");
@@ -579,7 +563,7 @@ int nestingdepth;
                                 int overwriteans = -1;
                                 do {
                                     printf("File %s exists.  Do you want to overwrite it (y/n) ?\n", Fname);
-                                    s = gets(AnsBuf);
+                                    s = fgets(AnsBuf, sizeof(AnsBuf), stdin);
                                     if (!s) {
                                         overwriteans = 0;
                                     } else {
@@ -1202,9 +1186,9 @@ char *SquirrelFile;
     fprintf(outfp, "Content-type: %s", ContentType);
     for (j=0; j<CParamsUsed; ++j) {
         fprintf(outfp, " ; ");
-        fprintf(outfp, CParams[j]);
+        fprintf(outfp, "%s", CParams[j]);
         fprintf(outfp, " = ");
-        fprintf(outfp, CParamValues[j]);
+        fprintf(outfp, "%s", CParamValues[j]);
     }
     fprintf(outfp, "\n\n"); 
     TranslateInputToOutput(InputFP, outfp, EncodingCode, ContentType);
@@ -1823,7 +1807,7 @@ char *ctype, *progname, *label;
         } else {
             printf("This message contains '%s'-format data.\nDo you want to view it using the '%s' command (y/n) [y] ? ", ctype, ShortCommand(progname));
         }
-        s = gets(AnsBuf);
+        s = fgets(AnsBuf, sizeof(AnsBuf), stdin);
         if (!s) return(0); /* EOF */
 	while (s && *s && isspace((unsigned char) *s)) ++s;
 	if (*s == 'y' || *s == 'Y' || !*s || *s == '\n') return(1);
@@ -1900,6 +1884,7 @@ char *hdr;
 }
 
 /* check the header given to see if it matches any in the KeyHeadList */
+void
 maybephead(hdr)
 char *hdr;
 {
@@ -2022,7 +2007,7 @@ int ShowLeadingWhitespace;
     if (lc2strcmp(charset, PrevCharset)) {
         char *s2, *charsetinuse;
 
-        strcpy(PrevCharset, charset);
+        strlcpy(PrevCharset, charset, sizeof(PrevCharset));
         for (s2=PrevCharset; *s2; ++s2) {
             if (isupper((unsigned char) *s2)) *s2 = tolower((unsigned char) *s2);
         }
@@ -2032,7 +2017,7 @@ int ShowLeadingWhitespace;
         }
     }
     if (ecode == ENCODING_NONE) {
-        printf(txt+1);
+        printf("%s", txt+1);
     } else {
         /* What follows is REALLY bogus, but all my encoding stuff is pipe-oriented right now... */
         MkTmpFileName(TmpFile);
@@ -2374,8 +2359,11 @@ char *Prefix;
 }
 
 int HasSavedTtyState=0;
+
 #if !defined(AMIGA) && !defined(MSDOS)
-#ifdef SYSV
+#if defined(HAVE_TCGETATTR)
+static struct termios MyTtyStateIn, MyTtyStateOut;
+#elif defined(SYSV)
 static struct termio MyTtyStateIn, MyTtyStateOut;
 #else
 static struct sgttyb MyTtyStateIn, MyTtyStateOut;
@@ -2385,7 +2373,10 @@ static struct sgttyb MyTtyStateIn, MyTtyStateOut;
 SaveTtyState() {
     /* Bogus -- would like a good portable way to reset the terminal state here */
 #if !defined(AMIGA) && !defined(MSDOS)
-#ifdef SYSV
+#if defined(HAVE_TCGETATTR)
+    tcgetattr(fileno(stdin), &MyTtyStateIn);
+    tcgetattr(fileno(stdout), &MyTtyStateOut);
+#elif defined(SYSV)
     ioctl(fileno(stdin), TCGETA, &MyTtyStateIn);
     ioctl(fileno(stdout), TCGETA, &MyTtyStateOut);
 #else
@@ -2398,8 +2389,13 @@ SaveTtyState() {
 
 RestoreTtyState() {
 #if !defined(AMIGA) && !defined(MSDOS)
-#ifdef SYSV
+#if defined(HAVE_TCGETATTR)
     if (HasSavedTtyState) {
+        tcsetattr(fileno(stdin), TCSANOW, &MyTtyStateIn);
+        tcsetattr(fileno(stdout), TCSANOW, &MyTtyStateOut);
+    }
+#elif defined(SYSV)
+    if (HasSavedTtyState) {
         ioctl(fileno(stdout), TCSETA, &MyTtyStateOut);
         ioctl(fileno(stdin), TCSETA, &MyTtyStateIn);
     }
@@ -2489,14 +2485,14 @@ char *name;
 {
 #ifdef AMIGA
     strcpy(name, "T:mmXXXXXX");
-    mktemp(name);
+    close(mkstemp(name));
 #else
 #ifndef MSDOS
     sprintf(name, "%s/mm.XXXXXX", tmproot);
-    mktemp(name);
+    close(mkstemp(name));
 #else
      strcpy(name, "TXXXXXX");
-     if (!mktemp(name))
+     if (!close(mkstemp(name)))
          name[0] = 0;
      else
          if (DoDebug) printf("temp name = \"%s\"\n", name);
@@ -2655,6 +2651,7 @@ char *s2;
 #endif
 }
 
+void
 PauseForUser() {
 #if defined(MSDOS) || defined(AMIGA)
     char Buf[100];
@@ -2681,7 +2678,16 @@ PauseForUser() {
 
 StartRawStdin() {
 #if !defined(AMIGA) && !defined(MSDOS)
-#ifdef SYSV
+#if defined(HAVE_TCGETATTR)
+    struct termios term;
+
+    if (tcgetattr(0, &term) == -1) /* get current (i.e. cooked) termio */
+        return -1;
+    term.c_lflag &= ~ICANON;    /* clear ICANON giving raw mode */
+    term.c_cc[VMIN] = 1;	/* set MIN char count to 1 */
+    term.c_cc[VTIME] = 0;	/* set NO time limit */
+    return tcsetattr(0, TCSADRAIN, &term);
+#elif defined(SYSV)
     struct termio   orterm, fterm;
     ioctl(0, TCGETA, &orterm);	/* get current (i.e. cooked) termio */
     fterm = orterm;		/* get termio to modify */
